package self.vector.ding.dao.mybatis.generator.plugin;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

public class DeleteByColumnPlugin extends PluginAdapter {
	
	private final String MethodName = "deleteByColumn";
	private final String Param_ColumnName = "columnName";
	private final String Param_ColumnValue = "columnValue";

	public boolean validate(List<String> warnings) {
		return true;
	}

	public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
			IntrospectedTable introspectedTable) {
		generateMethod(interfaze, topLevelClass, introspectedTable);
		return true;
	}

	public void generateMethod(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
		FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
		importedTypes.add(type);

		Method method = new Method();
		method.setName(MethodName);
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setReturnType(FullyQualifiedJavaType.getIntInstance());
		
		method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), Param_ColumnName,
				"@Param(\"" + Param_ColumnName + "\")"));
		method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), Param_ColumnValue,
				"@Param(\"" + Param_ColumnValue + "\")"));
		
		this.context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
		interfaze.addMethod(method);
	}

	public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
		generateXmlDocument(document, introspectedTable);
		return true;
	}

	public void generateXmlDocument(Document document, IntrospectedTable introspectedTable) {
		XmlElement answer = new XmlElement("delete"); //$NON-NLS-1$
		String methodName = MethodName;
		answer.addAttribute(new Attribute("id", methodName));
		answer.addAttribute(new Attribute("parameterType", "map"));

		this.context.getCommentGenerator().addComment(answer);

		StringBuilder sb = new StringBuilder();
        sb.append("delete from "); //$NON-NLS-1$
        sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
        sb.append(" where ");
		answer.addElement(new TextElement(sb.toString()));
		XmlElement chooseElement = new XmlElement("choose");
		XmlElement whenElement = new XmlElement("when");
		whenElement.addAttribute(new Attribute("test", "columnValue == null"));
		whenElement.addElement(new TextElement("${columnName} is null"));
		chooseElement.addElement(whenElement);
		whenElement = new XmlElement("when");
		whenElement.addAttribute(new Attribute("test", "columnValue != null"));
		whenElement.addElement(new TextElement("${columnName} = #{columnValue}"));
		chooseElement.addElement(whenElement);
		answer.addElement(chooseElement);

		XmlElement parentElement = document.getRootElement();
		parentElement.addElement(answer);
	}

	protected XmlElement getBaseColumnListElement(IntrospectedTable introspectedTable) {
		XmlElement answer = new XmlElement("include");
		answer.addAttribute(new Attribute("refid", introspectedTable.getBaseColumnListId()));
		return answer;
	}

	protected XmlElement getBlobColumnListElement(IntrospectedTable introspectedTable) {
		XmlElement answer = new XmlElement("include");
		answer.addAttribute(new Attribute("refid", introspectedTable.getBlobColumnListId()));
		return answer;
	}
}